package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.ActivityInfo;
import model.PhoneModelInfo;
import model.UserCommentInfo;
import model.UserInfo;
import service.ActivityService;
import service.PhoneService;
import service.UserService;

@WebServlet(name="/ManagePageServlet",urlPatterns="/manage")
public class ManagePageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String methodName= request.getParameter("method");
		System.out.println(methodName);
		if(methodName.equals("getCommentInfosList")) {
			UserService userService = new UserService();
			try {
				List<UserCommentInfo> commentInfosList = userService.getCommentInfosList();
				request.setAttribute("commentInfosList", commentInfosList);
				request.getRequestDispatcher("Comments_Manage.jsp").forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(methodName.equals("deleteComment")) {
			String Id = request.getParameter("userId");
			String userName = request.getParameter("userName");
			int userId = Integer.parseInt(Id);
			UserService userService = new UserService();
			try {
				boolean result = userService.deleteComment(userId);
				if(result) {
					//提示删除成功,并改变该用户信誉值
					boolean updateResult = userService.updateUserReputation(userName);
					if(updateResult) {
						response.getWriter().append("删除成功，用户信誉值-1");
					}
					
				}else {
					//提示删除失败
					response.getWriter().append("删除失败");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(methodName.equals("issueActivity")) {
			String activityName = request.getParameter("activityName");
			String ticketSum = request.getParameter("ticketSum");
			int tickets = Integer.parseInt(ticketSum);
			String activityDescription = request.getParameter("activityDescription");
			ActivityService acitivityService = new ActivityService();
			try {
				boolean result = acitivityService.addActivity(activityName, activityDescription, tickets);
				if(result) {
					response.getWriter().append("发布成功");
				}else {
					response.getWriter().append("发布失败");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(methodName.equals("updatePhonePrice")) {
			String phoneName = request.getParameter("phoneName");
			String price = request.getParameter("phonePrice");
			int phonePrice = Integer.parseInt(price);
			PhoneService phoneService = new PhoneService();
			try {
				boolean result = phoneService.updatePhonePrice(phoneName, phonePrice);
				if(result) {
					response.getWriter().append("修改成功");
				}else {
					response.getWriter().append("修改失败");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(methodName.equals("getPhoneNameList")) {
			PhoneService phoneService = new PhoneService();
			try {
				List<PhoneModelInfo> phoneNameList = phoneService.getPhoneNameList();
				request.setAttribute("phoneNameList", phoneNameList);
				request.getRequestDispatcher("PhonePrice_Update.jsp").forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(methodName.equals("getActivitiesSituationList")) {
			ActivityService activityService = new ActivityService();
			try {
				List<ActivityInfo> activityList=activityService.getActivityList();
				request.setAttribute("activityList", activityList);
				request.getRequestDispatcher("Activities_Manage.jsp").forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(methodName.equals("showActivityUsersList")) {
			boolean open =true;
			request.setAttribute("open", open);
			UserService userService = new UserService();
			try {
				List<UserInfo> joinActivityUsersList = userService.checkJoinActivityUsers();
				request.setAttribute("joinActivityUsersList", joinActivityUsersList);
				request.getRequestDispatcher("Activities_Situation.jsp").forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
